package glazedlists.chapter5;

import ca.odell.glazedlists.*;
import ca.odell.glazedlists.matchers.MatcherEditor;
import ca.odell.glazedlists.swing.*;
import ca.odell.issuezilla.Issue;
import ca.odell.issuezilla.IssuezillaXMLParser;
import glazedlists.chapter2.IssueComparator;
import glazedlists.chapter2.IssueTableFormat;
import glazedlists.chapter3.IssueTextFilterator;

import javax.swing.*;
import java.awt.*;
import java.io.InputStream;
import java.util.Collection;

public class IssuesBrowser5 {

    private EventList<Issue> issueEventList = new BasicEventList<>();

    public IssuesBrowser5(Collection<Issue> issues) {
        issueEventList.addAll(issues);
    }

    public void display() {
        SortedList<Issue> sortedIssues = new SortedList<>(issueEventList, new IssueComparator());
        UsersSelect usersSelect = new UsersSelect(sortedIssues);
        FilterList<Issue> userFilteredIssues = new FilterList<>(sortedIssues, usersSelect);
        JTextField filterEdit = new JTextField(10);
        IssueTextFilterator filterator = new IssueTextFilterator();
        MatcherEditor<Issue> matcherEditor = new TextComponentMatcherEditor<Issue>(filterEdit, filterator);
        FilterList<Issue> textFilteredIssues = new FilterList<>(userFilteredIssues, matcherEditor);

        AdvancedTableModel<Issue> tableModel = GlazedListsSwing.eventTableModelWithThreadProxyList(textFilteredIssues,
                new IssueTableFormat());
        JTable issueJTable = new JTable(tableModel);
        TableComparatorChooser.install(issueJTable, sortedIssues, TableComparatorChooser.MULTIPLE_COLUMN_MOUSE);
        JScrollPane issueTableScrollPane = new JScrollPane(issueJTable);

        JScrollPane usersListScrollPane = new JScrollPane(usersSelect.getJList());

        JPanel panel = new JPanel(new GridBagLayout());
        panel.add(new JLabel("Filter: "), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));
        panel.add(filterEdit, new GridBagConstraints(0, 1, 1, 1, 0.15, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));
        panel.add(new JLabel("Reported By: "), new GridBagConstraints(0, 2, 1, 1, 0.0, 0.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));
        panel.add(usersListScrollPane, new GridBagConstraints(0, 3, 1, 1, 0.15, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));
        panel.add(issueTableScrollPane, new GridBagConstraints(1, 0, 1, 4, 0.85, 1.0,
                GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                new Insets(5, 5, 5, 5), 0, 0));

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(540, 480);
        frame.getContentPane().add(panel);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        final Collection<Issue> issues;
        IssuezillaXMLParser parser = new IssuezillaXMLParser();
        try (InputStream inputStream = IssuesBrowser5.class.getResourceAsStream("/issues.xml")) {
            issues = parser.loadIssues(inputStream, null);
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }

        SwingUtilities.invokeLater(() -> {
            IssuesBrowser5 issuesBrowser = new IssuesBrowser5(issues);
            issuesBrowser.display();
        });
    }
}
